 /*-----------------------------------------------File Info------------------------------------------------
** File Name:               ACD_user.h  
** Last modified date:      2016.09.8
** Last version:            V0.1
** Description:             
**
**--------------------------------------------------------------------------------------------------------            
** Created date:            2016.09.8
** author:                  YLY
** Version:                 V0.1
** Descriptions:            ADC驱动
**--------------------------------------------------------------------------------------------------------*/
/*   硬件相关头文件     */
#include "bspADC.h"

void adcInit(tsCfgAdc *cfg)
{	
	gpioConfig(&cfg->io);
	
	adc_disable();
	
	/* enable ADC clock */
	rcu_periph_clock_enable(cfg->rcu);
	/* config ADC clock */
	rcu_adc_clock_config(RCU_ADCCK_APB2_DIV6);	
	
    /* ADC data alignment config */
    adc_data_alignment_config(ADC_DATAALIGN_RIGHT);
    /* ADC channel length config */
    adc_channel_length_config( ADC_REGULAR_CHANNEL, 1U);
    
    /* ADC trigger config */
    adc_external_trigger_source_config( ADC_REGULAR_CHANNEL, ADC_EXTTRIG_REGULAR_NONE); 
    /* ADC external trigger config */
    adc_external_trigger_config(ADC_REGULAR_CHANNEL, ENABLE);

    /* enable ADC interface */
    adc_enable();

    /* ADC calibration and reset calibration */
    adc_calibration_enable();	
}


/*!
    \brief      ADC sample
    \param[in]  none
    \param[out] none
    \retval     none
*/
uint16_t adcSample(tsCfgAdc *cfg)
{
    /* ADC regular channel config */
    adc_regular_channel_config(0U, cfg->channel, ADC_SAMPLETIME_7POINT5);
    /* ADC software trigger enable */
    adc_software_trigger_enable(ADC_REGULAR_CHANNEL);

    /* wait the end of conversion flag */
    while(!adc_flag_get(ADC_FLAG_EOC));
    /* clear the end of conversion flag */
    adc_flag_clear(ADC_FLAG_EOC);
    /* return regular channel sample value */
    return (adc_regular_data_read());
}



